package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.system.domain.IcEvent;
import com.ruoyi.system.mapper.IcEventMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.IcEventProcessMapper;
import com.ruoyi.system.domain.IcEventProcess;
import com.ruoyi.system.service.IIcEventProcessService;
import com.ruoyi.common.core.text.Convert;

/**
 * 事件管理流程Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-07-06
 */
@Service
public class IcEventProcessServiceImpl implements IIcEventProcessService 
{
    @Autowired
    private IcEventProcessMapper icEventProcessMapper;

    @Autowired
    private IcEventMapper icEventMapper;

    /**
     * 查询事件管理流程
     * 
     * @param epId 事件管理流程主键
     * @return 事件管理流程
     */
    @Override
    public IcEventProcess selectIcEventProcessByEpId(Long epId)
    {
        return icEventProcessMapper.selectIcEventProcessByEpId(epId);
    }

    /**
     * 查询事件管理流程列表
     * 
     * @param icEventProcess 事件管理流程
     * @return 事件管理流程
     */
    @Override
    public List<IcEventProcess> selectIcEventProcessList(IcEventProcess icEventProcess)
    {
        return icEventProcessMapper.selectIcEventProcessList(icEventProcess);
    }

    /**
     * 新增事件管理流程
     * 
     * @param icEventProcess 事件管理流程
     * @return 结果
     */
    @Override
    public int insertIcEventProcess(IcEventProcess icEventProcess)
    {
        icEventProcess.setCreateTime(DateUtils.getNowDate());
        return icEventProcessMapper.insertIcEventProcess(icEventProcess);
    }

    /**
     * 修改事件管理流程
     * 
     * @param icEventProcess 事件管理流程
     * @return 结果
     */
    @Override
    public int updateIcEventProcess(IcEventProcess icEventProcess)
    {
        return icEventProcessMapper.updateIcEventProcess(icEventProcess);
    }

    /**
     * 批量删除事件管理流程
     * 
     * @param epIds 需要删除的事件管理流程主键
     * @return 结果
     */
    @Override
    public int deleteIcEventProcessByEpIds(String epIds)
    {
        return icEventProcessMapper.deleteIcEventProcessByEpIds(Convert.toStrArray(epIds));
    }

    /**
     * 删除事件管理流程信息
     * 
     * @param epId 事件管理流程主键
     * @return 结果
     */
    @Override
    public int deleteIcEventProcessByEpId(Long epId)
    {
        return icEventProcessMapper.deleteIcEventProcessByEpId(epId);
    }

    @Override
    public List<IcEventProcess> selectIcEventProcessListByEId(Long eventId) {
        return icEventProcessMapper.selectIcEventProcessListByEId(eventId);
    }

    @Override
    public AjaxResult passEventProcess(IcEventProcess process) {
        String processName = DictUtils.getDictLabel("ic_event_process", String.valueOf(process.geteProcess()));
        Subject subject = SecurityUtils.getSubject();
        if (StringUtils.isBlank(processName)) {
            throw new RuntimeException("通过类型不存在");
        }
        if (process.geteAudit() != 1L) {
            throw new RuntimeException("不是通过审核请求");
        }
        SysUser principal = (SysUser) subject.getPrincipal();
        process.setUserId(principal.getUserId());
        int i = insertIcEventProcess(process);
        Long processKey = process.geteProcess() + 1;
        processName = DictUtils.getDictLabel("ic_event_process", String.valueOf(processKey));
        if (StringUtils.isNotBlank(processName)) {
            IcEvent event = icEventMapper.selectIcEventByEId(process.geteId());
            event.seteProcess(String.valueOf(processKey));
            event.seteAudit(0L);
            icEventMapper.updateIcEvent(event);
        }
        // 更新到
        if (i > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }
}
